package com.example.thirdhomework

import android.content.Intent
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.example.thirdhomework.databinding.ActivityMainBinding
import java.io.Serializable

const val KEY1 = "key1"

data class Person(val name: String, val age: Int) : Serializable
data class Student(val name: String, val age: Int, val teacher: Person) : Serializable
class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        ViewCompat.setOnApplyWindowInsetsListener(binding.main) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        binding.buttonFirst.setOnClickListener {
            val intent = Intent(this, FirstActivity::class.java)
            val student =  Student(binding.editTextText.text.toString(), 20, Person("Teacher", 30))
            intent.putExtra(KEY1, student)
            startActivity(intent)
        }

        val launcher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
            if (result.resultCode == RESULT_OK) {
                val data: Intent? = result.data
                val text = data?.getStringExtra(KEY1)
                binding.editTextText.setText(text)
            }
        }
        binding.buttonSecond.setOnClickListener {
            val intent = Intent(this, SecondActivity::class.java)
            launcher.launch(intent)
        }

        val launcherPhoto = registerForActivityResult(ActivityResultContracts.TakePicturePreview()) {
            binding.imageView.setImageBitmap(it)
        }
        binding.buttonPhoto.setOnClickListener{
            launcherPhoto.launch(null)
        }

        binding.buttonThird.setOnClickListener{
            val intent = Intent(this, ThirdActivity::class.java)
            startActivity(intent)
        }
    }
}